<?php
namespace app\tickets\controller;

use app\common\controller\Common;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Tickets extends Base
{

    /**
     * 门票配置
     * @author zhixuedong
     * @date 2021/1/30/030
     */
    public function config(){
        $config = Db::name("tickets_config")->find();
        $tickets_type = $config['tickets_type'];
        $tickets_type = explode("|",$tickets_type);
        $tickets_value = [];
        foreach ($tickets_type as $k=>$v){
            $v = explode(":",$v);
            $tickets_value[$v[0]] = $v[1];
        }


        $this->assign("config",$config);
        return $this->fetch('config');
    }

    /**
     * 票种列表
     * @author zhixuedong
     * @date 2021/1/20/020
     */
    public function index(){
        $common = new Common();

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if(!empty($_GET['keyword'])){
            $where['title'] = ['like',"%".$_GET['keyword']."%"];
        }
        if(!empty($_GET['status'])){
            $where['status'] = $_GET['status'];
        }
        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $publicTable = "tickets_tickets";
                $newSql = $common->public_select($publicTable,$where,null,$start,$limit);
                $list = $newSql->order("sort desc,id desc")->select();
                foreach ($list as $k=>$v){
                    $v['thumb'] = empty($v['thumb']) ? "/static/images/nopic-107.png" : $v['thumb'];
                    $list[$k]['thumb'] = "<img src='".$v['thumb']."' style='max-height:50px;max-width:58px;'>";
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $count = $common->public_select($publicTable,$where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch('index');
    }

    /**
     * 添加视图&详细页面
     * @author zhixuedong
     * @date 2021/1/20/020
     */
    public function addTickets(){
        //票类型
        $common = new Common();
        $tickets_type = $common->publicAttribute("tickets_type");
        $this->assign("tickets_type",$tickets_type);
        $enable_status = $common->publicAttribute("enable_status");
        $this->assign("enable_status",$enable_status);
        if(empty($_GET['id'])){
            return $this->fetch('create');
        }else{
            //获取门票详细信息
            $publicTable = "tickets_tickets";
            $where['id'] = $_GET['id'];
            $newSql = $common->public_select($publicTable,$where);
            $detail = $newSql->find();
            $this->assign("detail",$detail);
            return $this->fetch('update');
        }

    }

    /**
     * 添加&修改操作
     * @author zhixuedong
     * @date 2021/1/20/020
     */
    public function doTickets(){
        $common = new Common();
        Db::startTrans();
        try{
            $rule = [
                ["title", "require", "标题不能为空"],
                ["type", "require", "请选择门票类型"],
                ["validity", "require", "请输入门票有效期"],
                ["validity", "require", "请输入价格"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            $table = "tickets_tickets";
            $id = $common->handle($table,$value);
            if(!empty($value['id'])){
                $log['remark'] = "用户【" . Session::get("user_name") . "】更新门票,关联id【".$id."】";
            }else{
                $log['remark'] = "用户【" . Session::get("user_name") . "】添加门票,关联id【".$id."】";
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
            Db::commit();
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            Db::rollback();
        }
        return $black;
    }

    /**
     * 删除操作
     * @author zhixuedong
     * @date 2021/1/20/020
     */
    public function delTickets(){
        $common = new Common();
        try {
            $id = $_POST['id'];
            $publicTable = "tickets_tickets";
            $where['id'] = ['in', $id];
            $field = "id,title";
            $newSql = $common->public_select($publicTable,$where,$field);
            $navList = $newSql->select();
            foreach ($navList as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除门票种类【".$v['title']."】删除id【" . $v['id'] . "】";
                $common->addLog($log);
            }
            $newSql = $common->public_select($publicTable,$where,$field);
            $newSql->delete();
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}